import { redirect } from 'react-router-dom';
import { MyRouteObject, checkAdminAuth } from './index';
import { lazy } from 'react';

// 懒加载管理端组件
const AdminLayout = lazy(() => import('@/layouts/AdminLayout'));
const AdminDashboard = lazy(() => import('@/pages/admin/Dashboard'));
const ProblemEdit = lazy(() => import('@/pages/admin/problem/Edit'));
const ProblemDetail = lazy(() => import('@/pages/admin/problem/Detail'));
const ProblemList = lazy(() => import('@/pages/admin/problem/List'));
const ProblemSetList = lazy(() => import('@/pages/admin/problem/ProblemSetList'));
const ProblemSetDetail = lazy(() => import('@/pages/admin/problem/ProblemSetDetail'));
const ProblemSetEdit = lazy(() => import('@/pages/admin/problem/ProblemSetEdit'));
const SubmissionList = lazy(() => import('@/pages/share/submission/List'));
const SubmissionDetail = lazy(() => import('@/pages/share/submission/Detail'));
const ContestList = lazy(() => import('@/pages/admin/contest/List'));
const ContestCreate = lazy(() => import('@/pages/admin/contest/Create'));
const ContestEdit = lazy(() => import('@/pages/admin/contest/Edit'));
const ContestDetail = lazy(() => import('@/pages/admin/contest/Detail'));
const ContestAnnouncements = lazy(() => import('@/pages/admin/contest/Announcements'));
const ContestProblems = lazy(() => import('@/pages/admin/contest/Problems'));
const ContestReview = lazy(() => import('@/pages/admin/contest/Review'));
const SystemSettings = lazy(() => import('@/pages/admin/setting'));
const UserManagement = lazy(() => import('@/pages/admin/user'));
const TeamManagement = lazy(() => import('@/pages/admin/user/TeamManagement'));
// 问卷系统页面
const QuestionBankList = lazy(() => import('@/pages/admin/question/QuestionBankList'));
const QuestionBankDetail = lazy(() => import('@/pages/admin/question/QuestionBankDetail'));
const QuestionList = lazy(() => import('@/pages/admin/question/QuestionList'));
const QuestionEditor = lazy(() => import('@/pages/admin/question/QuestionEditor'));
const PaperListQuestion = lazy(() => import('@/pages/admin/question/PaperList'));
const ArticleList = lazy(() => import('@/pages/admin/content/ArticleList'));

// 管理端路由配置
export const adminRoutes: MyRouteObject[] = [
  {
    path: '/admin',
    element: <AdminLayout />,
    breadcrumb: '管理员',
    loader: () => checkAdminAuth() ? null : redirect('/login'), // 全局管理员权限检查
    children: [
      {
        index: true,
        element: <AdminDashboard />,
        breadcrumb: '管理控制台',
      },
      // 系统设置路由
      {
        path: 'setting',
        element: <SystemSettings />,
        breadcrumb: '系统配置',
      },
      {
        path: 'problem',
        breadcrumb: '题目管理',
        children: [
          {
            index: true,
            loader: () => redirect('/admin/problem/list'),
          },
          {
            path: 'list',
            element: <ProblemList />,
            breadcrumb: '题目列表',
          },
          {
            path: 'create',
            element: <ProblemEdit />,
            breadcrumb: '创建题目',
          },
          {
            path: 'edit/:id',
            element: <ProblemEdit />,
            breadcrumb: '编辑题目',
          },
          {
            path: ':id',
            element: <ProblemDetail />,
            breadcrumb: '题目详情',
          },
          {
            path: 'problem-sets',
            element: <ProblemSetList />,
            breadcrumb: '题单管理',
          },
          {
            path: 'problem-set/:id',
            element: <ProblemSetDetail />,
            breadcrumb: '题单详情',
          },
          {
            path: 'problem-set/create',
            element: <ProblemSetEdit />,
            breadcrumb: '创建题单',
          },
          {
            path: 'problem-set/edit/:id',
            element: <ProblemSetEdit />,
            breadcrumb: '编辑题单',
          },

        ],
      },
      // 比赛管理路由
      {
        path: 'contest',
        breadcrumb: '比赛管理',
        children: [
          {
            index: true,
            loader: () => redirect('/admin/contest/list'),
          },
          {
            path: 'list',
            element: <ContestList />,
            breadcrumb: '比赛列表',
          },
          {
            path: 'create',
            element: <ContestCreate />,
            breadcrumb: '创建比赛',
          },
          {
            path: 'edit/:id',
            element: <ContestEdit />,
            breadcrumb: '编辑比赛',
          },
          {
            path: ':id',
            element: <ContestDetail />,
            breadcrumb: '比赛详情',
          },
          {
            path: ':id/announcements',
            element: <ContestAnnouncements />,
            breadcrumb: '比赛公告',
          },
          {
            path: ':id/problems',
            element: <ContestProblems />,
            breadcrumb: '题目管理',
          },
          {
            path: ':id/review',
            element: <ContestReview />,
            breadcrumb: '比赛批阅',
          },
        ],
      },
      
      // 其他管理路由暂时注释掉，等组件实现后再打开
      {
        path: 'question',
        breadcrumb: '问卷管理',
        children: [
          {
            index: true,
            loader: () => redirect('/admin/question/banks'),
          },
          {
            path: 'banks',
            element: <QuestionBankList />,
            breadcrumb: '题库管理',
          },
          {
            path: 'bank/:id',
            element: <QuestionBankDetail />,
            breadcrumb: '题库详情',
          },
          {
            path: 'questions',
            element: <QuestionList />,
            breadcrumb: '题目列表',
          },
          {
            path: 'question/create',
            element: <QuestionEditor />,
            breadcrumb: '创建题目',
          },
          {
            path: 'question/edit/:id',
            element: <QuestionEditor />,
            breadcrumb: '编辑题目',
          },
          {
            path: 'papers',
            element: <PaperListQuestion />,
            breadcrumb: '试卷列表',
          },
        ],
      },
      // exam路由已移除（exam模块已删除）
      // 用户管理路由
      {
        path: 'submission',
        breadcrumb: '提交管理',
        children: [
          {
            index: true,
            element: <SubmissionList />,
            breadcrumb: '提交列表',
            loader: () => checkAdminAuth() ? null : redirect('/login'),
          },
          {
            path: ':id',
            element: <SubmissionDetail />,
            breadcrumb: '提交详情',
            loader: () => checkAdminAuth() ? null : redirect('/login'),
          },
        ],
      },
      {
        path: 'user',
        breadcrumb: '用户管理',
        children: [
          {
            index: true,
            element: <UserManagement />,
            breadcrumb: '用户列表',
          },
          {
            path: 'list',
            element: <UserManagement />,
            breadcrumb: '用户列表',
          },
          {
            path: 'teams',
            element: <TeamManagement />,
            breadcrumb: '团队管理',
          }
        ]
      },
      {
        path: 'paper',
        breadcrumb: '试卷管理（重定向到新问卷系统）',
        children: [
          {
            index: true,
            loader: () => redirect('/admin/question/papers'),  // 重定向到新问卷系统
          },
          {
            path: 'list',
            loader: () => redirect('/admin/question/papers'),
          },
          {
            path: 'create',
            loader: () => redirect('/admin/question/paper/create'),
          },
          {
            path: 'edit/:id',
            loader: () => redirect('/admin/question/paper/edit/:id'),
          },
        ],
      },
      // 内容管理
      {
        path: 'content',
        breadcrumb: '内容管理',
        children: [
          {
            path: 'articles',
            element: <ArticleList />,
            breadcrumb: '文章管理',
          },
        ],
      },
    ],
  },
]; 